package com.py.credit.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


import java.util.Optional;
import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.logs.SystemLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.SysUser;
import com.py.credit.entity.enums.AuthorizationSearchTableFieldEnum;
import com.py.credit.service.AuthorizationConfigService;
import com.py.credit.service.BatchLogService;
import com.py.credit.service.RemoteBaseService;
import com.py.credit.utils.MapCache;
import com.py.framework.core.common.Page;
import com.py.framework.core.helper.HttpServletHelper;

/**
 * 
 * @ClassName: MyTestController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author jdzc
 * @date 2020年6月4日 下午4:29:39
 *
 */
@RequestMapping("/MyTest")
@RestController
public class MyTestController {
	private final static String FRONT_TABLE_AUTH__ID = "authListTable";
	private static Logger logger = Logger.getLogger(MyTestController.class);
	@Autowired
	private BatchLogService batchLogService;

	@Autowired
	private AuthorizationConfigService authorizationConfigService;

	@Autowired
	private RemoteBaseService remoteBaseService;

	@Value("${absolutepath}")
	private String absolutepath;

	/**
	 * 
	 * @Title: logDownload
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @param @param batchId
	 * @param @param response
	 * @param @return    设定文件
	 * @return RestServiceResult<Object>    返回类型
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/logDownload.json")
	public RestServiceResult<Object> logDownload(
			@RequestParam(defaultValue = "", required = true, value = "batchId") String batchId,
			HttpServletResponse response) {
		RestServiceResult<Object> result = new RestServiceResult<Object>();
		try {
			batchLogService.logDownload(batchId, response, absolutepath);
			return result;
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("日志文件下载异常，请稍后再试！");
			ExceptionLogger.error("BatchLogController#logDownload",
					"日志文件下载异常ERROR:" + e.getMessage() + ",batchId=" + batchId + ",path=" + absolutepath, e);
			return result;
		}
	}
	/**
	 * 
	 * @Title: getAuthList
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @param @param deptId
	 * @param @param condition
	 * @param @param page
	 * @param @param request
	 * @param @return    设定文件
	 * @return RestServiceResult<List<Map<String,Object>>>    返回类型
	 * @throws
	 */
	@RequestMapping(value = "/getAuthList.json")
	public RestServiceResult<List<Map<String, Object>>> getAuthList(
			@RequestParam(value = "deptId", defaultValue = "", required = true) String deptId,
			@RequestParam(value = "condition", defaultValue = "", required = true) String condition, Page page,
			HttpServletRequest request) {
		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
		String token = HttpServletHelper.getUserToken(request);
		String userName = "";
		String[] configColumns = null;
		SysUser userInfo = user.getData();
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			if (null != userInfo) {
				if ("系统管理员".equals(Optional.ofNullable(user.getData())
						.map(SysUser->
						SysUser.getRoleName())
						.orElse(null))) {
					userName = "";				
				} else {
					userName = Optional.ofNullable(user.getData()).map(SysUser->SysUser.getRoleName()).orElse("KNOWN");
					SystemLogger.info(userName);
				}
				configColumns = (String[]) MapCache.get(userInfo.getId() + "_" + FRONT_TABLE_AUTH__ID);
			} else {
				ExceptionLogger.error("AuthorizationSearchController#getAuthList", "未获取到当前用户信息！");
			}
		} else {
			ExceptionLogger.error("AuthorizationSearchController#getAuthList", "未获取到当前用户信息！");
		}
		List<String> showColumns = (configColumns == null || configColumns.length == 0)
				? AuthorizationSearchTableFieldEnum.getDefaultShowColumnEN()
				: Arrays.asList(configColumns);
		String[] additionalColumns = { "id" };
		List<Map<String, Object>> tableList = authorizationConfigService.getByConditionForPage(userName, showColumns,
				additionalColumns, condition, page);
		SystemLogger.info(tableList.toString());
		return new RestServiceResult<List<Map<String, Object>>>(page.getTotalCount(), tableList);
	}
	public static void main(String[] args) {
		String value = null;
		String hello = "hello";
		String play = "play";
		// 如果为非空，返回 Optional 描述的指定值，否则返回空的 Optional。
		Optional<String> a = Optional.ofNullable(hello);
		Optional<String> b = Optional.ofNullable(value);
		// orElse 如果存在该值，返回值， 否则返回 other。
		System.out.println(a.orElse(value));//1
		System.out.println(a.orElse(hello));//2
		System.out.println(a.orElse(play));//3
		System.out.println(a);//3
		System.out.println("++++++++++++++++++");
		System.out.println(b.orElse("空指针"));
		System.out.println("------------------");
		System.out.println(b.orElse(value));//5
		System.out.println(b.orElse(hello));//6
		System.out.println(b.orElse("")); //7
		System.out.println(b);     
		ExceptionLogger.error("AuthorizationSearchController#getAuthList", "未获取到当前用户信息1！");
		ExceptionLogger.error("AuthorizationSearchController#getAuthList", "未获取到当前用户信息2！");
	}

}
